var token = "";
var searchResult = document.getElementById("searchResult");
var searchNullP = document.getElementById("searchNullP");
var searchData = document.getElementById("searchData");
var sidList = document.getElementById("sidList");
var warehouseList = document.getElementById("inList");
var list = document.getElementById("list");
var funName = "";
var pageNum = 1;
var itemNum = 20;
var countNum = 0;
var type = "出库";

var textStr = "";
var isShishi = "";
var pageFlag = "";
var pageflag = "";

(function(e) {

	mui.init({
		pullRefresh: {
			container: '#list',
			down: {
				style: 'circle',
				callback: pulldownRefresh
			},
			up: {
				auto: true,
				contentrefresh: '正在加载...',
				callback: pullupRefresh
			}
		}
	});

	setPxPerRem();
	//	changeLineHeight();
	mui.plusReady(function() {
		textStr = getQueryString("textStr");
		isShishi = getQueryString("isShishi");
		pageflag = getQueryString("pageflag");
		console.log(textStr);
		if(!isEmpty(textStr)) {

			$("#queryType").html(textStr);
			$("#projectTitle").html(textStr);

			if(textStr == "入库查询") {
				funName = "PhoneGetRKMain";
				$("#orderNo").html("入库单号");
				type = "入库";
				showEle(warehouseList);
			} else {
				$("#orderNo").html("出库单号");
				$("#searchInput").attr('placeholder', "出库单号查询");
				funName = "PhoneGetCKMain";
				showEle(sidList);
			}
		}

		if(isShishi) {
			funName = funName + "_rt";
			showEle(sidList);
			showEle(warehouseList);
		}

	});
})(mui);

var isRefresh = false;

function pulldownRefresh() {
	removeLi("list");
	isRefresh = true;
	pageNum = 1;
	isFirst = true;
	if(isHaveNet()) {
		getWarehouseIn();
	} else {
		mui.toast("无网络连接！");
	}
}

//上拉加载
var isLoad = false;
var isFirst = true;

function pullupRefresh() {
	isLoad = true;
	if(isHaveNet()) {
		getWarehouseIn();
	} else {
		mui.toast("无网络连接！");
	}
}
var keyword = "";
var currentSid = "";
var currentWarehouse = "";
var dataList;
var lengthNum = 0;
var lookInfo = document.getElementById("lookInfo");
var statusStr = "";
function getWarehouseIn() {
	token = localStorage.getItem("pid");
	statusStr = currentSid;
	if(textStr == "入库查询") {
		statusStr = currentWarehouse;
	}

	if(isEmpty(token)) {
		mui.toast("登录认证失败，请退出后重试");
		return;
	}

	if(isShishi && isGet) {
		if(isEmpty(currentSid) || isEmpty(currentWarehouse)) {
			mui.toast("请选择仓库后在查询");
			return;
		}
	}

	plus.nativeUI.showWaiting();
	if(!isFirst && !isEmpty(dataList)) {
		if(dataList.length > lengthNum) {
			var length = dataList.length;
			if(dataList.length > lengthNum + itemNum) {
				length = lengthNum + itemNum;
			}

			for(var i = lengthNum; i < length; i++) {
				createItem(dataList[i], i);
			}
			lengthNum += itemNum;
			mui('#list').pullRefresh().endPullup(false);
		} else {
			mui('#list').pullRefresh().endPullup(true);
		}
		plus.nativeUI.closeWaiting();
		return;
	}

	var pjson = {
		status: statusStr,
		pid: token,
		code: keyword
	}
	var jsonData = {
		op: funName,
		pjson: JSON.stringify(pjson)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {

		//		if(!isGet){
		//			getSidList();
		//		} else {
		//			plus.nativeUI.closeWaiting();
		//		}
		isFirst = false;
		plus.nativeUI.closeWaiting();
		showEle(searchResult);
		if(!isEmpty(data)) {
			hiddenEle(searchNullP);
			showEle(searchData);
			showEle(lookInfo);
			dataList = data;
			countNum = dataList.length;
			if(countNum > itemNum) {
				lengthNum = itemNum;
			} else {
				lengthNum = countNum;
			}

			for(var i = 0; i < lengthNum; i++) {
				createItem(dataList[i], i);
			}
			//			lengthNum += itemNum;
		} else {
			$("#searchNull").html(keyword);
			hiddenEle(searchData);
			showEle(searchNullP);
			mui.toast("暂无数据");
		}

		if(isRefresh) {
			mui('#list').pullRefresh().endPulldown(false);
			isRefresh = false;
		}

		if(isLoad) {
			isLoad = false;
			mui('#list').pullRefresh().endPullup(false);
		}
		if(pageNum == 1) {
			mui('#list').pullRefresh().endPullup(false);
		}
		pageNum++;
	});
}
var currentId = "";
var currentCode = "";
mui("#list").on("tap", "li", function() {
	var checkBoxs = list.getElementsByTagName("input");
	for(var i = 0; i < checkBoxs.length; i++) {
		checkBoxs[i].checked = false;
	}
	var currentBox = this.getElementsByTagName("input")[0];
	currentBox.checked = true;
	currentId = this.id;
	currentCode = this.className;
});

$("#seleceInfo").click(function() {
	if(isEmpty(currentId)) {
		mui.toast("请选择条目后在查看");
		return;
	}
	var postdata = {
		pageflag: "page_password.html",
		sid: currentSid,
		warehouse: currentWarehouse,
		itemID: currentId,
		type: type,
		itemCode: currentCode
	}

	var change = {
		pageUrl: "page_itemInfo.html",
		postData: postdata,
		aniShow: "fade-in"
	}

	pageChange(change);
});

var insertDiv = document.getElementById("insertDiv");

function createItem(row, index) {
	var createLi = document.createElement("li");
	var ischeck = "";
	var id = index + 1;
	var Code = row.SIM_InvoiceNo;
	var status = row.status;
	var itemCode = row.SIM_Code;
	var ShipName = row.SIM_ShipName;
	var BOU_Name = row.BOU_Name;
	var Status = row.sm_name;
	var CangKu = row.S_Name;
	var order_sn = row.SIM_TiDanCode;
//	if(funName == "PhoneGetCKMain") {
//		id = index + 1;
//		Code = row.SOM_Code;
//		Status = row.sm_name;
//		CangKu = row.S_Name;
//		order_sn = row.SOM_TiDanCode;
//	}
	if(index == 0) {
		currentId = itemCode;
		currentCode = status;
		ischeck = "checked='checked'";
	}

	createLi.id = itemCode;
	createLi.className = status;
	createLi.innerHTML = '<cite class="mui-checkbox"><input type="checkbox" ' + ischeck + ' name="" id="" value="" /></cite>' +
		'<cite style="width: 1.3rem;margin-left: 0.3rem;">' + id + '</cite>' +
		'<cite style="width: 3.1rem;">' + Status + '</cite>' +
		'<cite id="" style="width: 2.6rem;">'+ status +'</cite>' +
		'<cite id="" style="width: 2.6rem;">'+ ShipName +'</cite>' +
		'<cite style="width: 3.6rem;">' + Code + '</cite>' +
		'<cite id="" style="width: 3.6rem;">'+ BOU_Name +'</cite>' + 
		'<cite style="width: 3.1rem;">' + CangKu + '</cite>' +
		'<cite style="width: 4rem;">' + order_sn + '</cite>';
	document.body.appendChild(createLi);
	insertBefore(createLi, insertDiv);
}

$("#queryBtn").click(function() {
	keyword = $("#searchInput").val().trim();
	if(isEmpty(keyword) && !isShishi) {
		mui.toast("请输入发票号、船名、提单号、油品公司查询");
		return;
	}
	pulldownRefresh();
});

var sid = document.getElementById("sid");
var warehouse = document.getElementById("warehouse");
var isGet = false;

function getSidList() {
	token = localStorage.getItem("token");
	var p = {
		op: "provider_list",
		token: token
	}
	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		isGet = true;
		var htmlStr = '<option value="-1" selected>请选择供应商</option>';
		if(!isEmpty(data)) {
			for(var i = 0; i < data.length; i++) {
				htmlStr += '<option value="' + data[i].o_tid + '">' + data[i].o_name + '</option>'
			}
		}
		sid.innerHTML = htmlStr;
	});
}

var isLoad = false;

function getWarehouseList() {
	var p = {
		op: "warehouse_list",
		token: token,
		data: {
			sid: currentSid,
			keyword: ""
		}
	}
	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}
	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		isLoad = true;
		plus.nativeUI.closeWaiting();
		var htmlStr = '<option value="-1" selected="selected">请选择仓库</option>';
		if(!isEmpty(data)) {
			for(var i = 0; i < data.length; i++) {
				htmlStr += '<option value="' + data[i].id + '" >' + data[i].S_Name + '</option>'
			}
		}
		warehouse.innerHTML = htmlStr;
	});
}

sid.addEventListener('change', function() {
	currentSid = this.value;
	//	if(currentSid == "-1" || isEmpty(currentSid)) {
	//		mui.toast("请先选择供销商");
	//		return;
	//	}
	//	warehouse.innerHTML = '<option value="-1" selected="selected">请选择仓库</option>';
	//	getWarehouseList();
	pulldownRefresh();

}, false);

warehouse.addEventListener('change', function() {
	currentWarehouse = this.value;
	pulldownRefresh();
}, false);